package com.tianqu.tms.core.taglib;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;




/**
 * 分页标签
 * @author liugang
 * update #107
 */
public class PagingTag extends TagSupport {

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = -6328279409411520449L;
	
	protected static final String CRLF = System.getProperty("line.separator");
	//url
	private String gotoURI;
	//当前页面序号
	private String pageNo;
	//页面排序的title序号
	private String sortKey;
	//是否是降序
	private String asc;
	//记录总数
	private String totalCount;
	//当前页面数据开始序号
	private String dateFrom;
	//当前页面数据结束序号
	private String dateTo;
	//一页显示的行数
	private String maxIndex = "2";
	//表头需要有选择功能的列序号,默认是0表示任何一列都不需要有这个功能
	private String checkBoxKey = "0";
	// 导出Excel Action
	private String actionExport;
	
	/**
	 * 分页显示
	 *  <div class="right-Page">
            <div class="page">
                第&nbsp;50&nbsp;页中&nbsp;1&nbsp;-&nbsp;10&nbsp;条&nbsp;|&nbsp;<a href="#">&lt;&lt;上一页</a>&nbsp;<a href="#">下一页&gt;&gt;</a>&nbsp;|
            </div>
        </div>
	 */
	private void setPagingHtml(StringBuffer sb,String gotoURI_Previous,String gotoURI_Next,String current){
		sb.append("<div class=\"right-Page\">");
		sb.append(CRLF);
		sb.append("<div class=\"page\">");
		sb.append(CRLF);
		sb.append("第&nbsp;");
		sb.append(totalCount);
		sb.append("&nbsp;条中&nbsp;");
		//sb.append("<strong>");
		sb.append(dateFrom.concat("&nbsp;-&nbsp;").concat(dateTo));
		sb.append("&nbsp;条&nbsp;|&nbsp;");
		if(gotoURI_Previous!=null) {
			sb.append("<a href=\""+gotoURI_Previous+"\">");
			sb.append("&lt;&lt;上一页");
			sb.append("</a>");
		} else {
			sb.append("&lt;&lt;上一页");
		}
		
		sb.append("&nbsp;");
		if(gotoURI_Next!=null) {
			sb.append("<a href=\""+gotoURI_Next+"\">");
			sb.append("下一页&gt;&gt;");
			sb.append("</a>");
		} else {
			sb.append("下一页&gt;&gt;");
		}
		sb.append("&nbsp;|");
		if (actionExport != null && actionExport != "" && current != null && current != "") {
            sb.append("<a href=\""+current+"\">");
            sb.append("导出Excel&gt;&gt;");
            sb.append("</a>");
		}
		sb.append(CRLF);
		sb.append("</div>");
		sb.append(CRLF);
		sb.append("</div>");
		
	}
	
	/**
	 * 标签开始
	 */
	public int doStartTag() throws JspException {
		StringBuffer sb = new StringBuffer();
		int maxIndexTem = Integer.parseInt(maxIndex);
		int pageNoTem = Integer.parseInt(pageNo);
		int totleCountTem = Integer.parseInt(totalCount);
		String current = null;
		if(totleCountTem == 0) {
			pageNoTem = 1;
		}
		if (gotoURI.indexOf("?") != -1) {
			gotoURI = gotoURI.concat("&");
		} else {
			gotoURI = gotoURI.concat("?");
		}
		if (actionExport == null || actionExport == "") {
			actionExport = "";
		} else {
	        if (actionExport.indexOf("?") != -1) {
	            actionExport = actionExport.concat("&");
	        } else {
	            actionExport = actionExport.concat("?");
	        }
		}
		
		String gotoURI_Next = null;
		String gotoURI_Previous = null;
		String tmp = "maxIndex="+maxIndex+"&sortKey="+sortKey+"&asc="+asc+"&checkBoxKey="+checkBoxKey;
		if (pageNoTem > 1 ) {
			gotoURI_Previous = gotoURI.concat(tmp).concat("&").concat("pageNo="+ (pageNoTem - 1));
		}
		if ((pageNoTem * maxIndexTem) < totleCountTem) {
			gotoURI_Next = gotoURI.concat(tmp).concat("&").concat("pageNo="+ (pageNoTem + 1));
		}
		if (actionExport != null && actionExport != "") {
			current = actionExport.concat(tmp).concat("&").concat("pageNo="+ (pageNoTem));
		}
		setPagingHtml(sb,gotoURI_Previous,gotoURI_Next,current);

		try {
			pageContext.getOut().println(sb.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}
		return super.doStartTag();
	}
	public String getGotoURI() {
		return gotoURI;
	}
	public void setGotoURI(String gotoURI) {
		this.gotoURI = gotoURI;
	}
	public String getMaxIndex() {
		return maxIndex;
	}
	public void setMaxIndex(String maxIndex) {
		this.maxIndex = maxIndex;
	}
	public String getPageNo() {
		return pageNo;
	}
	public void setPageNo(String pageNo) {
		this.pageNo = pageNo;
	}
	public String getTotalCount() {
		return totalCount;
	}
	public void setTotalCount(String totalCount) {
		this.totalCount = totalCount;
	}
	public String getSortKey() {
		return sortKey;
	}
	public void setSortKey(String sortKey) {
		this.sortKey = sortKey;
	}
	public String getAsc() {
		return asc;
	}
	public void setAsc(String asc) {
		this.asc = asc;
	}
	public String getDateFrom() {
		return dateFrom;
	}
	public void setDateFrom(String dateFrom) {
		this.dateFrom = dateFrom;
	}
	public String getDateTo() {
		return dateTo;
	}
	public void setDateTo(String dateTo) {
		this.dateTo = dateTo;
	}

	public String getCheckBoxKey() {
		return checkBoxKey;
	}
	public void setCheckBoxKey(String checkBoxKey) {
		this.checkBoxKey = checkBoxKey;
	}
	
    public String getActionExport() {
        return actionExport;
    }

    public void setActionExport(String actionExport) {
        this.actionExport = actionExport;
    }
}
